<template>
<!-- 搜索页 -->
  <div class="search">
    <!-- 头部 -->
    <div class="search-head">
            <van-icon name="arrow-left" size="0.8rem" @click="$router.go(-1)" />
            <div class="icon">
            <input type="text" v-model="keyword" placeholder="爱情鲜花" />
            <van-icon name="search" />
            </div>
            <van-button type="primary" round color="#ff734c" @click="search">搜索</van-button>
    </div>
    <!-- 热门搜索 -->
    <div class="hot">
            <div class="text">热门搜索</div>
            <div class="hot-bot">
                <van-button type="primary" color="#E9ECF0" size="small">红玫瑰</van-button>
                <van-button type="primary" color="#E9ECF0" size="small">百合</van-button>
                <van-button type="primary" color="#E9ECF0" size="small">蛋糕</van-button>
                <van-button type="primary" color="#E9ECF0" size="small">康乃馨</van-button>
            </div>
    </div>
    <!-- 历史记录 -->
    <div class="history">
      <div class="text">历史记录</div>
      <div class="history-bot">
                <van-button
                 v-for="k in his"
                 :key='k'
                 @click="history(k)"
                 type="primary"
                 color="#E9ECF0"
                 size="small">
                 {{k}}
                 </van-button>
            </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      keyword: '',
      // 获取缓存记录
      his: localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
    }
  },
  methods: {
    history (k) {
      this.keyword = k
      this.search()
    },
    search () {
      // 存缓存 先取后存
      let his = localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
      his.unshift(this.keyword)
      // 数组去重
      his = [...new Set(his)]
      // 限制长度
      if (his.length > 5) {
        his.pop()
      }
      // 存缓存 以json格式储存
      localStorage.setItem('his', JSON.stringify(his))
      const keyText = this.keyword
      console.log(keyText)
      this.$router.push({
        path: '/itemlists',
        query: {
          word: keyText
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-head {
  margin-top: 0.2rem;
  padding-bottom: .3rem;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-bottom: .1rem solid #f3f5f7;
  .icon {
    align-content: center;
    position: relative;
    .van-icon {
      position: absolute;
      font-size: 0.6rem;
      left: 0.3rem;
      top: 0.2rem;
    }
    input {
      height: 0.9rem;
      width: 5rem;
      border: none;
      background-color: #f3f5f7;
      border-radius: 0.6rem;
      font-size: 0.4rem;
      padding-left: 1rem;
    }
  }
  .van-button {
    height: 0.8rem;
    font-size: .4rem;
    text-align:center;
  }
}
.history{
  .text{
     font-size: .4rem;
  }
  .history-bot{
     margin: 0;
        padding: 0;
        .van-button{
              transform: translateY(-.3rem);
              margin: 0 .2rem;
        .van-button__text{
              color: #555;
        }
    }
  }
}
.hot{
    margin: .3rem 0 0 .2rem;
    .text{
    font-size: .4rem;
    }
    .hot-bot{
        margin: 0;
        padding: 0;
        .van-button{
              transform: translateY(-.3rem);
              margin: 0 .2rem;
        .van-button__text{
              color: #555;
        }
    }
    }
}
</style>
